package com.hooper.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hooper.hosp.mapper.HospitalSetMapper;
import com.hooper.hosp.model.hosp.HospitalSet;
import com.hooper.hosp.service.HospitalSetService;
import com.hooper.hosp.vo.order.SignInfoVo;
import com.hooper.hospital.common.exception.HospitalException;
import com.hooper.common.util.MD5;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.regex.Pattern;

import static com.hooper.hospital.common.constant.HospSetConstant.*;
import static com.hooper.hospital.common.result.ResultCodeEnum.*;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2022/09/24/13:42
 */
@Slf4j
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {
    @Override
    public boolean lockOrUnlock(Long id, Integer status) {
        if (id == null || status == null) {
            throw new HospitalException(PARAM_EMPTY);
        }
        if (status != 0 && status != 1) {
            throw new HospitalException(PARAM_ERROR);
        }

        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        return this.updateById(hospitalSet);
    }

    @Override
    public boolean saveOne(HospitalSet hospitalSet) {
        validationCheck(hospitalSet);
        hospitalSet.setSignKey(encryptedKey());
        return baseMapper.insert(hospitalSet) == 1;
    }

    @Override
    public boolean updateOneById(HospitalSet newSet) {
        validationCheck(newSet);
        return baseMapper.updateById(newSet) == 1;
    }

    @Override
    public String getSignKeyByHoscode(String hoscode) {
        log.info("hoscode:{}", hoscode);
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HospitalSet::getHoscode, hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if (hospitalSet == null) {
            throw new HospitalException(OBJECT_IS_NULL);
        }
        return hospitalSet.getSignKey();
    }

    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        if (StringUtils.isEmpty(hoscode)) {
            throw new HospitalException(PARAM_EMPTY);
        }
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HospitalSet::getHoscode, hoscode);
        HospitalSet hospitalSet = this.getOne(wrapper);
        if (ObjectUtils.isEmpty(hospitalSet)) {
            throw new HospitalException(NO_SUCH_DATA);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        return signInfoVo;
    }

    /**
     * Check if the obj is validated
     *
     * @param hospitalSet
     */
    public void validationCheck(HospitalSet hospitalSet) {
        if (hospitalSet == null) {
            throw new HospitalException(OBJECT_IS_NULL);
        }
        String phone = hospitalSet.getContactsPhone();
        String code = hospitalSet.getHoscode();
        String name = hospitalSet.getHosname();
        String apiUrl = hospitalSet.getApiUrl();

        if (StringUtils.isAnyBlank(phone, code, name, apiUrl)) {
            throw new HospitalException(PARAM_EMPTY);
        }

        if (!Pattern.matches(VALIDATE_PHONE_PATTERN, phone)) {
            throw new HospitalException(PHONE_NOT_VALIDATE);
        }
    }

    public String encryptedKey() {
        return MD5.encrypt(System.currentTimeMillis() + EXTRA_SALT + new Random(1000).nextInt());
    }
}
